/**
 * 
 */
package com.coder520.topic.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.management.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.coder520.topic.dao.TabTopicSensitiveMapper;
import com.coder520.topic.dao.TabTopicSpeakMapper;
import com.coder520.topic.entity.TabTopicSpeak;
import com.coder520.topic.service.TabTopicSpeakService;

/**
 * @author zhaoyibo
 *
 */
@Service("tabTopicSpeakService")
public class TabTopicSpeakServiceImpl implements TabTopicSpeakService{
	private static final String DELETEMARK="N";
	private static final String NODELETEMARK="Y";
	@Autowired
	private TabTopicSpeakMapper tabTopicSpeakMapper;
	
	@Autowired
	private TabTopicSensitiveMapper tabTopicSensitiveMapper;
	/* (non-Javadoc)
	 * @see com.coder520.topic.service.TabTopicSpeakService#query(java.util.Map)
	 * @date:2018年12月17日 下午6:05:09
	 */
	@Override
	public List<TabTopicSpeak> query(Map<String, String> map) {
		// TODO Auto-generated method stub
		return tabTopicSpeakMapper.query(map);
	}

	/* (non-Javadoc)保存方法进行敏感词校验
	 * @see com.coder520.topic.service.TabTopicSpeakService#save(com.coder520.topic.entity.TabTopicSpeak)
	 * @date:2018年12月17日 下午6:41:14
	 */
	@Override
	public int save(TabTopicSpeak tabTopicSpeak) {
		// TODO Auto-generated method stub
		int type=0;
		String speakId = tabTopicSpeak.getSpeakId();
		
		String content = tabTopicSpeak.getContent();
		List<String> contents = tabTopicSensitiveMapper.queryContent();
		for (String string : contents) {
//			如果有敏感词，那么直接返回
			if (content.indexOf(string)!=-1) {
				return -1;//如果ajax获取-1那么说明存在敏感词
			}
		}
		if (StringUtils.isEmpty(speakId)) {
			tabTopicSpeak.setSpeakId(UUID.randomUUID().toString());
			tabTopicSpeak.setDeleteMark(NODELETEMARK);
			type=tabTopicSpeakMapper.insertSelective(tabTopicSpeak);
		}else {
			type=tabTopicSpeakMapper.updateByPrimaryKeySelective(tabTopicSpeak);
		}
		return type;
	}

	/* (non-Javadoc)
	 * @see com.coder520.topic.service.TabTopicSpeakService#get(java.lang.String)
	 * @date:2018年12月17日 下午7:04:51
	 */
	@Override
	public TabTopicSpeak get(String speakId) {
		// TODO Auto-generated method stub
		return tabTopicSpeakMapper.selectByPrimaryKey(speakId);
	}
	
	/* (non-Javadoc)
	 * @see com.coder520.topic.service.TabTopicSpeakService#delete(java.lang.String)
	 * @date:2018年12月17日 下午7:11:32
	 */
	@Override
	public int delete(String speakId) {
		// TODO Auto-generated method stub
		
		TabTopicSpeak tabTopicSpeak=new TabTopicSpeak();
		tabTopicSpeak.setSpeakId(speakId);
		tabTopicSpeak.setDeleteMark(DELETEMARK);
		return tabTopicSpeakMapper.updateByPrimaryKeySelective(tabTopicSpeak);
	}
	
}
